<?php

namespace app\admin\controller\project;

use app\admin\controller\Base;
use think\Db;

/**
 * 用户组管理
 * Class GroupController
 * @package app\admin\controller\auth
 */
class Projectuser extends Base
{
    /**
     * 主页面
     * @return mixed
     */
    public function index()
    {
        $this->assign('project_status', config('param.project_status'));
        return view();
    }
    public function list_project()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();
        if (empty($post['keyword']) || $post['keyword'] == '') {
        } else {
            $map[] = ['project.title', 'like', '%' . $post['keyword'] . '%'];
        }
        $data['count'] = Db::name('project')
            ->where($map)
            ->count();
        $data['data'] = Db::name('project')
            ->where($map)
            ->order([ 'id desc'])
            ->select();
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }
    public function list_user()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();
        $data['count'] = Db::name('admin')
            ->where($map)

            ->count();
        $data['data'] = Db::name('admin')
            ->where($map)
            ->where('status',1)
            ->order([ 'id desc'])
            ->select();
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }
    public function lists()
    {
        $map = array();
        $map2=array();
        $data = array();
        $post = $this->request->param();
        if (empty($post['title']) || $post['title'] == '') {
        } else {
            $map[] = ['project.title', 'like', '%' . $post['title'] . '%'];
            $map2[] = ['admin.nickname', 'like', '%' . $post['title'] . '%'];
        }

        $data['count'] = Db::view('project_user','id,project_id,uid,c_time,user_type,work_wage')
            ->view('admin','nickname','project_user.uid=admin.id')
            ->view('project','title','project_user.project_id=project.id')
            ->where($map)
            ->whereor($map2)
            ->count();

        $data['data'] = Db::view('project_user','id,project_id,uid,c_time,user_type,work_wage')
            ->view('admin','nickname','project_user.uid=admin.id')
            ->view('project','title','project_user.project_id=project.id')
            ->where($map)
            ->whereor($map2)
            // ->fetchSql(true)
            ->order('id desc')->page(input("param.page"), input('param.limit'))->select();
        $user_type=config('param.project_user_type');
        foreach ($data['data'] as &$info) {
            $info['c_time'] =date('Y-m-d', $info['c_time']);
            $info['user_type']=$user_type[$info['user_type']];
        }
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            $validate = new \app\admin\validate\Projectuser();
            if (!$validate->check($post)) {
                $this->error($validate->getError());
            }
            $map[]=['project_id','=',$post['project_id']];
            $map[]=['uid','=',$post['uid']];
            $map[]=['user_type','=',$post['user_type']];
            $info=Db::name('project_user')->where($map)->find();
            if($info){
                $this->error('对应的项目已经存在同角色用户,不能重复添加');
            }
            Db::startTrans();
            try {
                $post['c_time']=time();
                $post['work_wage']=DB::name('user_base')->where('uid',$post['uid'])->value('work_wage');
                Db::name('project_user')->insert($post);
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        } else {
            $user_type=config('param.project_user_type');
            $this->assign('user_type',$user_type);
            return view();
        }
    }


    public function delete()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            if (!isset($post['id'])) {
                $this->error('请求参数错误');
            }
            Db::startTrans();
            try {
                $info=Db::name('project_user')->where(['id'=>$post['id']])->delete();
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        }
    }



    public function editform()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            //单个字段修改
            if(count($post)==2){

                Db::startTrans();
                try {
                    Db::name('project')->where(['id'=>$post['id']])->update($post);
                    Db::commit();
                    $status = 1;
                } catch (\Exception $e) {
                    Db::rollback();
                    $status = 0;
                }
            }else{
                $validate = new \app\admin\validate\Project();
                if (!$validate->check($post)) {
                    $this->error($validate->getError());
                }
                Db::startTrans();
                try {
                    Db::name('infoview')->where(['id'=>$post['info_id']])->update(['info' => $this->request->param('info','',null)]);
                    unset($post['info']);
                    $post['s_time'] = strtotime($post['s_time']);
                    $post['e_time'] = strtotime($post['e_time']);
                    Db::name('project')->where(['id'=>$post['id']])->update($post);
                    Db::commit();
                    $status = 1;
                } catch (\Exception $e) {
                    Db::rollback();
                    $status = 0;
                }
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        } else {
            $this->assign('project_status', config('param.project_status'));
            $info = Db::name('project')
                ->where('id', input('param.id'))
                ->find();
            $info['s_time'] == 0 ? $info['s_time'] = '' : $info['s_time'] = date('Y-m-d', $info['s_time']);
            $info['e_time'] == 0 ? $info['e_time'] = '' : $info['e_time'] = date('Y-m-d', $info['e_time']);
            $info['info']=Db::name('infoview')
                ->where('id',$info['info_id'])
                ->value('info');
            $this->assign('info', $info);
            return view('add');
        }

    }


}